<template>
  <div>
    <el-form ref="slideForm" :model="slideForm" :rules="rules" label-width="80px" size="medium">
      <el-form-item prop="title" label="标题">
        <el-input v-model="slideForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item prop="description" label="描述">
        <el-input type="textarea" rows="3" resize="none" v-model="slideForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item prop="imgPath" label="图片">
        <el-upload
          ref="slideImg"
          action="slideImg"
          accept=".png, .jpg, .jpeg"
          :show-file-list="false"
          :limit="1"
          :before-upload="beforeUpload"
          :http-request="uploadFile"
        >
          <div class="avatar">
            <img v-if="slideForm.imgPath" :src="slideForm.imgPath">
            <i v-else class="el-icon-plus"></i>
          </div>
          <div slot="tip" class="el-upload__tip">只能上传png、jpg、jpeg格式的文件，且不超过5M</div>
        </el-upload>
      </el-form-item>
      <el-form-item prop="webPath" label="跳转链接">
        <el-input v-model="slideForm.webPath" placeholder="跳转链接地址"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn-box">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button type="primary" size="medium" @click="submitForm">提 交</el-button>
    </div>
  </div>
</template>

<script>
  import {upload} from "../../assets/api/getData";

  export default {
    name: "slideForm",
    props: {
      data: {
        type: Object,
        default: {}
      },
      isUpdate: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        slideForm: {
          title: "",
          description: "",
          imgPath: "",
          webPath: "",
          isShow: 1,
        },
        rules: {
          imgPath: {required: true, message: "请上传图片", trigger: "change"},
          title: {required: true, message: "必填", trigger: "blur"},
        }
      }
    },
    created() {
      if(this.isUpdate){
        for (let v in this.slideForm){
          this.slideForm[v] = this.data[v];
        }
      }
    },
    methods: {
      beforeUpload(file) {
        let fileSize = file.size;
        if (fileSize <= 0) {
          this.$message.warning('上传文件大小不能为空!');
          return false
        }
        if (fileSize > 5 * 1024 * 1024) {
          this.$message.warning('上传文件大小不能大于5M!');
          return false
        }
      },
      async uploadFile(param) {
        let res = await upload({
          directory: "slide",
          file: param.file,
        });
        this.slideForm.imgPath = res.data;
        this.$refs['slideImg'].clearFiles();
      },
      cancel(){
        this.$emit("cancel");
      },
      submitForm() {
        this.$refs["slideForm"].validate(async valid => {
          if (valid) {
            this.$emit("submitSlideForm", this.slideForm);
          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="less" scoped>
  .btn-box {
    text-align: center;
  }
</style>
